package city.spring.modules.personnel.controller;

import city.spring.modules.personnel.entity.EmployeeRewardPunishmentEntity;
import city.spring.modules.personnel.service.EmployeeRewardPunishmentService;
import city.spring.modules.personnel.service.EmployeeService;
import city.spring.modules.personnel.vo.EmployeeRewardPunishmentEntityVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.NonNull;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller：员工奖惩
 *
 * @author 潘小梅
 * @date 2020-04-08 15:12:03
 */
@RestController
@RequestMapping("/employee_reward_punishment")
public class EmployeeRewardPunishmentController extends ApiController {
    private final EmployeeRewardPunishmentService employeeRewardPunishmentService;
    private final EmployeeService employeeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<EmployeeRewardPunishmentEntity, ?>> entityOrderFields;

    public EmployeeRewardPunishmentController(EmployeeRewardPunishmentService employeeRewardPunishmentService, EmployeeService employeeService) {
        this.employeeRewardPunishmentService = employeeRewardPunishmentService;
        this.employeeService = employeeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getId);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getEmployeeId);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getExecutionTime);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getType);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getMoney);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getReason);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getOperatorId);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getRemarks);
        entityOrderFields.add(EmployeeRewardPunishmentEntity::getGmtCreate);
    }

    /**
     * 获取全部的 <strong>员工奖惩</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EmployeeRewardPunishmentEntity entity) {
        LambdaQueryChainWrapper<EmployeeRewardPunishmentEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>员工奖惩</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, EmployeeRewardPunishmentEntityVO entity) {
        QueryWrapper<? extends EmployeeRewardPunishmentEntity> queryWrapper = buildQuery(entity);
        return success(employeeRewardPunishmentService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));

    }
//    @GetMapping
//    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EmployeeRewardPunishmentEntity entity, Date executionTime1, Date executionTime2) {
//        LambdaQueryChainWrapper<EmployeeRewardPunishmentEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        if (executionTime1 != null && executionTime2 != null) {
//            lambdaQuery.ge(EmployeeRewardPunishmentEntity::getExecutionTime, executionTime1);
//            lambdaQuery.le(EmployeeRewardPunishmentEntity::getExecutionTime, executionTime2);
//        }
//        Page<EmployeeRewardPunishmentEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        for (EmployeeRewardPunishmentEntity record : page.getRecords()) {
//            try {
//                EmployeeEntity employeeEntity = employeeService.getById(record.getEmployeeId());
//                record.setEmployeename(employeeEntity.getName());
//            } catch (Exception ignore) {
//                record.setEmployeeId(null);
//                record.setEmployeename("< 找不到员工 >");
//            }
//        }
//        return success(page);
//    }

    /**
     * 获取一个 <strong>员工奖惩</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        Page<EmployeeRewardPunishmentEntityVO> page = new Page<>();
        QueryWrapper<EmployeeRewardPunishmentEntityVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        IPage<EmployeeRewardPunishmentEntityVO> iPage = employeeRewardPunishmentService.listPage(page, wrapper);
        List<EmployeeRewardPunishmentEntityVO> records = iPage.getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>员工奖惩</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody EmployeeRewardPunishmentEntity entity, @NonNull Principal user) {
        employeeRewardPunishmentService.saveEmployeeRewardPunishment(entity, user);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>员工奖惩</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody EmployeeRewardPunishmentEntity entity) {
        entity.setId(id);
        employeeRewardPunishmentService.updateEmployeeRewardPunishment(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>员工奖惩</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        employeeRewardPunishmentService.deleteEmployeeRewardPunishment(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>员工奖惩</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        employeeRewardPunishmentService.deleteEmployeeRewardPunishment(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<EmployeeRewardPunishmentEntity> buildLambdaQuery(EmployeeRewardPunishmentEntity entity) {
        LambdaQueryChainWrapper<EmployeeRewardPunishmentEntity> lambdaQuery = employeeRewardPunishmentService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), EmployeeRewardPunishmentEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getEmployeeId()), EmployeeRewardPunishmentEntity::getEmployeeId, entity
                .getEmployeeId());
        lambdaQuery.like(entity.getExecutionTime() != null, EmployeeRewardPunishmentEntity::getExecutionTime, entity.getExecutionTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), EmployeeRewardPunishmentEntity::getType, entity.getType());
        lambdaQuery.like(entity.getMoney() != null, EmployeeRewardPunishmentEntity::getMoney, entity.getMoney());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReason()), EmployeeRewardPunishmentEntity::getReason, entity.getReason());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOperatorId()), EmployeeRewardPunishmentEntity::getOperatorId, entity
                .getOperatorId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemarks()), EmployeeRewardPunishmentEntity::getRemarks, entity
                .getRemarks());
        lambdaQuery.like(entity.getGmtCreate() != null, EmployeeRewardPunishmentEntity::getGmtCreate, entity.getGmtCreate());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<? extends EmployeeRewardPunishmentEntity> buildQuery(EmployeeRewardPunishmentEntityVO entity) {
        QueryWrapper<? extends EmployeeRewardPunishmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getEmployeeId()), "t1.employee_id", entity.getEmployeeId());
        wrapper.like(StringUtils.isNotBlank(entity.getPersonName()), "t2.name", entity.getPersonName());
        wrapper.like(StringUtils.isNotBlank(entity.getOperatorName()), "t3.nickname", entity.getOperatorName());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "t1.type", entity.getType());
        wrapper.eq(entity.getExecutionTime() != null, "t1.execution_time", entity.getExecutionTime());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        return wrapper;
    }

}
